<?php
use think\Request; //获取请求类型
 
function gettree($data, $tid,$num=0){
	$num+=1;
	$nbsp='';
	if($tid!=0){
		$nbsp.=$nbsp.'&nbsp;&nbsp;┠&nbsp;&nbsp';
	}
	$html = '';
	$hide=array('0'=>'隐藏','1'=>'显示');
	$hides=array('1'=>'隐藏','0'=>'显示');
	foreach($data as $k => $v){
		if($v['tid'] == $tid){
			$html 	.= "\r\n<tr id=\"id".$v['id']."\" class=\"gradeA footable-even category".$num."\">";
			$html 	.=	"<td class=\"categoryTitle\">".$nbsp.$v['id']."</td>";
			$html 	.=	"<td class=\"categoryTitle\">".$nbsp.$v['title']."</td>";
			$html 	.=	"<td>".$v['controller']."</td>";
			$html 	.=	"<td>".$v['function']."</td>";
			$html 	.=	"<td>".$hide[$v['hide']]."</td>";
			$html 	.=	'<td><div class="btn-group">';
			$html 	.=	'<button data-toggle="dropdown" class="btn btn-primary btn-xs dropdown-toggle">操作 <span class="caret"></span></button><ul class="dropdown-menu">';
			$html 	.=	"<li><a href=".url('add',['id'=>$v['id']]).">修改</a></li><li><a href=".url('hide',['id'=>$v['id']]).">".$hides[$v['hide']]."</a></li>".'<li class="divider"></li>';
			$html  	.=	"<li><a href=\"javascript:;\" onclick=\"del(".$v['id'].")\">删除</a></li></td></tr>";
			$html 	.= getTree($data, $v['id'],$num);
			$html 	=$html;
		}
	}
	return $html;
}
function category($data, $tid,$selected,$num=0){
	$num+=1;
	$nbsp='';
	$selecteds='';
	if($num!=1){
		for ($i=1; $i <$num ; $i++) { 
			$nbsp.='&nbsp;&nbsp;&nbsp;&nbsp;';
		}
		$nbsp.='┠&nbsp;&nbsp;';
	}
	$html = '';
	foreach($data as $k => $v){
		if($v['tid'] == $tid){
			if($selected==$v['id']){
				$selecteds='selected="selected"';
			}else{
				$selecteds='';
			}
			$html 	.=	"\r\n".'<option value="'.$v['id'].'" '.$selecteds.'>'.$nbsp.$v['title'].'</option>';
			$html 	.= category($data, $v['id'],$selected,$num);
			$html 	=$html;
		}
	}
	return $html;
}
//输出option列表形式
function get_tree_option($data, $tid) {
    $stack = array($tid);
    $child = array();
    $added = array();
    $options = array();
    $obj = array();
    $loop = 0;
    $depth = -1;
    foreach($data as $node) {
        $pid = $node['tid'];
        if(!isset($child[$pid])) {
            $child[$pid] = array();
        }
        array_push($child[$pid], $node['id']);
        $obj[$node['id']] = $node;
    }

    while (count($stack) > 0) {    
        $id = $stack[0];
        $flag = false;
        $node = isset($obj[$id]) ? $obj[$id] : null;
        if (isset($child[$id])) {
            for($i = count($child[$id]) - 1; $i >= 0; $i--) {
                array_unshift($stack, $child[$id][$i]);
            }
            $flag = true;
        }
        if ($id != $tid && $node && !isset($added[$id])) {
            $node['depth'] = $depth;
            $options[] = $node;
            $added[$id] = true;
        }
        if($flag == true){
            $depth++;
        } else {
            if($node) {
                for ($i = count($child[$node['tid']]) - 1; $i >= 0; $i--) {
                    if ($child[$node['tid']][$i] == $id) {
                        array_splice($child[$node['tid']], $i, 1);
                        break;
                    }
                } 
                if(count($child[$node['tid']]) == 0) {
                    $child[$node['tid']] = null;
                    $depth--;
                }
            }
            array_shift($stack);
        }
        $loop++;
        if($loop > 5000) return $options;
    }
    unset($child);
    unset($obj);
    return $options;
}

//文章分类输出option列表形式
function get_tree_option1($data, $tid) {
    $stack = array($tid);
    $child = array();
    $added = array();
    $options = array();
    $obj = array();
    $loop = 0;
    $depth = -1;
    foreach($data as $node) {
        $pid = $node['prev'] = isset($node['prev']) ? $node['prev'] : 0;
        if(!isset($child[$pid])) {
            $child[$pid] = array();
        }
        array_push($child[$pid], $node['id']);
        $obj[$node['id']] = $node;
    }

    while (count($stack) > 0) {    
        $id = $stack[0];
        $flag = false;
        $node = isset($obj[$id]) ? $obj[$id] : null;
        if (isset($child[$id])) {
            for($i = count($child[$id]) - 1; $i >= 0; $i--) {
                array_unshift($stack, $child[$id][$i]);
            }
            $flag = true;
        }
        if ($id != $tid && $node && !isset($added[$id])) {
            $node['depth'] = $depth;
            $options[] = $node;
            $added[$id] = true;
        }
        if($flag == true){
            $depth++;
        } else {
            if($node) {
                for ($i = count($child[$node['prev']]) - 1; $i >= 0; $i--) {
                    if ($child[$node['prev']][$i] == $id) {
                        array_splice($child[$node['prev']], $i, 1);
                        break;
                    }
                } 
                if(count($child[$node['prev']]) == 0) {
                    $child[$node['prev']] = null;
                    $depth--;
                }
            }
            array_shift($stack);
        }
        $loop++;
        if($loop > 5000) return $options;
    }
    unset($child);
    unset($obj);
    return $options;
}

// 百度编辑器
function ueditor($name='',$num=1){
	if(!$name){$name='content';}
	$html='<script type="text/javascript" charset="utf-8" src="' . SITE_PATH . '/public/style/admin/plugins/ueditor/ueditor.config.js"></script>';
	$html.='<script type="text/javascript" charset="utf-8" src="' . SITE_PATH . '/public/style/admin/plugins/ueditor/ueditor.all.js"></script>';
	$html.='<script type="text/javascript" src="' . SITE_PATH . '/public/style/admin/plugins/ueditor/lang/zh-cn/zh-cn.js"></script>';
	$html.='<script type="text/javascript">';

	for ($i=1;$i<=$num;$i++) {
	    $extra = $i != 1 ? $i : '';
        $html.="var ue".$extra."=UE.getEditor('".$name.$extra."');";
    }
    $html.='</script>';

	return $html;
}
 
 // 保留价格后面2位数
function _money($money){
    if(empty($money)){
        return '0.00';
    }
   return number_format($money/100,2,'.',''); 
}

function haveimg($str)
{
    $dian = stripos($str,'<img ');
    if($dian === false){
        return _substr($str);
    } else {
        return substr($str,0,$dian).' [图片]';
    }
}

function biaoqing($str = '')
{
    if (!$str) {
        return $str;
    }
    if (strpos($str, '<img') === false) {
        return $str;
    }
    $qian = config('appconfig.url');
    $qian = '';
    $str = str_replace('<img src=../../res/img/emotion', '<img src="' . $qian . '/public/style/admin/liaotian/img/emotion', $str);
    if (strpos($str, '.png"') === false) {
        $str = str_replace('.png', '.png"', $str);
    }
    return $str;
}

function getcontent($id) {
    $privatechat = db('privatechat')->where('id', $id)->find();

    if ($privatechat['type']==2) {
        if (file_exists('apis/'.$privatechat['content'])) {
            $a=file_get_contents('apis/'.$privatechat['content']);
            $b=base64_encode($a);
            $privatechat['content']=$b;
        }
    }

    return $privatechat;
}
